/*
	$Id: Parser.js 33 2008-05-01 21:39:39Z beave $
*/

function Table() {
	if (arguments.length == 0) {
		throw new Error("columns not specified");
	}

	this.columns = new Array();

	for (var i = 0; i < arguments.length; i++) {
		this.columns[i] = arguments[i];
	}

	this.content = new Array();
	this._count = 0;
}


Table.prototype.append = function() {
	if (arguments.length == 0){
		throw new Error("values not specified");
	}

	if (arguments.length > this.columns.length) {
		throw new Error("wrong count of values");
	}

	aNewStroke = new Array();

	if (typeof(arguments[0]) == "object") {
		for (var i = 0; i < this.columns.length; i++) {
			aNewStroke.push(arguments[0][this.columns[i]]);
		}
	} else {
		for (var i = 0; i < arguments.length; i++) {
			aNewStroke.push(arguments[i]);
		}
	}

	this.content.push(aNewStroke);

	this._count = this.content.length;
	
	if (this.content.length == 1) {
		this.offset(0);
		this.line = 1;
	}
}


Table.prototype.offset = function(intN) {
	for (var i = 0; i < this.columns.length; i++) {
		this[this.columns[i]] = this.content[intN][i];
	}

	this.line = ++intN;
}


Table.prototype.menu = function(funcProcess) {
	iCurrentLine = this.line;

	for (var i = this.line - 1; i < this.content.length; i++) {
		this.offset(i);
		funcProcess(this);
	}

	this.line = iCurrentLine;
	this.offset(--this.line);
}


Table.prototype.toHash = function(strColumn) {
	if (strColumn == "" || strColumn == undefined) {
		throw new Error("column not specified");
	}

	hResult = new Hash();

	var sColumns = "";

	for (var i = 0; i < this.columns.length; i++) {
		sColumns += '"'+this.columns[i]+'"';
		if (i + 1 < this.columns.length) {
			sColumns += ","
		}
	}

	this.menu(function(tOriginal){
		if(!hResult[tOriginal[strColumn]]){
			hResult[tOriginal[strColumn]] = eval("new Table("+sColumns+")");
		}
		hResult[tOriginal[strColumn]].append(tOriginal);
	});

	return hResult;
}



function Hash() {
	for (var i = 0; i < arguments.length; i += 2) {
		this[arguments[i]] = arguments[i+1];
	}
}


Hash.prototype._recheck = function() {
		for (var sProperty in this) {
			if (this[sProperty] == null || this[sProperty] == "") {
				delete(this[sProperty]);
			}
		}
	}


Hash.prototype.forEach = function(funcProcess) {
	this._recheck;

	for (var sProperty in this) {
		if (sProperty != "_count" && sProperty != "_keys"  && sProperty != "_recheck" && sProperty != "forEach" && sProperty != "toString" && this[sProperty] != undefined) {
			funcProcess({ key: sProperty, value: this[sProperty]});
		}
	}
}


Hash.prototype._count = function() {
	iCount = 0;

	this.forEach(function() {iCount++});

	return iCount;
}


Hash.prototype._keys = function() {
	aResult = new Array();
	
	this.forEach(function(objData) {
		aResult.push(objData.key);
	});
	
	return aResult;
}


Hash.prototype.toString = function() {
	if (this._count()) {
		sResult = new String();
		
		this.forEach(function(objData) {
			if (sResult != "") {
				sResult+=", ";
			}
			
			sResult += objData.key+": "+objData.value;
		});

		return "{ "+sResult+" }";
	}
}


Math.uuid = function() {
	return new UUID();
}


function Request() {
	sQuery = location.search.substring(1);
	aPairs = sQuery.split("&");
	
	for (var i = 0; i < aPairs.length; i++) {
		pos = aPairs[i].indexOf('=');
		sName = aPairs[i].substring(0,pos);
		sValue = aPairs[i].substring(pos+1);
		this[sName] = decodeURIComponent(sValue);
	}
}
